package com.taobao.ucenter.service.impl;

import com.alibaba.fastjson.JSON;
import com.taobao.ucenter.mapper.MenuMapper;
import com.taobao.ucenter.mapper.UserAuthMapper;
import com.taobao.ucenter.model.dto.AuthParamsDto;
import com.taobao.ucenter.model.dto.UserExt;
import com.taobao.ucenter.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    UserAuthMapper userAuthMapper;

    @Autowired
    MenuMapper menuMapper;
    @Autowired
    ApplicationContext applicationContext;



    //传入的请求认证的参数就是AuthParamsDto
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //将传入的json转成AuthParamsDto对象
        AuthParamsDto authParamsDto = null;
        try {
            authParamsDto = JSON.parseObject(s, AuthParamsDto.class);
        } catch (Exception e) {
            throw new RuntimeException("请求认证参数不符合要求");
        }

        //认证类型，有password，wx。。。
        String authType = authParamsDto.getAuthType();


        //根据认证类型从spring容器取出指定的bean
        String beanName = authType+"_authservice";
        AuthService authService = applicationContext.getBean(beanName, AuthService.class);
        //调用统一execute方法完成认证
        UserExt userExt = authService.execute(authParamsDto);
        //封装xcUserExt用户信息为UserDetails
        //根据UserDetails对象生成令牌
        UserDetails userPrincipal = getUserPrincipal(userExt);

        return userPrincipal;
    }

    /**
     * 查询用户信息
     */
    public UserDetails getUserPrincipal(UserExt userExt){
        String password = userExt.getPassword();
        //权限
        String[] authorities=  {"test"};
        //根据用户id查询用户的权限
//        List<Menu> menus = menuMapper.selectPermissionByUserId(tbUser.getTbId());
//        if(menus.size()>0){
//            List<String> permissions =new ArrayList<>();
//            menus.forEach(m->{
//                //拿到了用户拥有的权限标识符
//                permissions.add(m.getCode());
//            });
//            //将permissions转成数组
//            authorities = permissions.toArray(new String[0]);
//        }

        userExt.setPassword(null);

        //将用户信息转json
        // 第一是可以扩展UserDetails，使之包括更多的自定义属性，
        // 第二也可以扩展username的内容 ，比如存入json数据内容作为username的内容。相比较而言，
        // 方案二比较简单还不用破坏UserDetails的结构，采用方案二
        String userJson = JSON.toJSONString(userExt);
        if(password == null){
            password = "";
        }
        UserDetails userDetails = User.withUsername(userJson).password(password).authorities(authorities).build();
        return  userDetails;
    }


}
